﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using AppManager.Model;
using AppManager.DAL;
using System.Runtime.Remoting.Messaging;
using AppManager.IDAL;
using AppManager.IBLL;

namespace AppManager.BLL
{
    /// <summary>
    /// 使用public来修饰，是为了不把这些方法公开给其它层（web层），所有的controller，只能使用它的实现类中的方法，而不能直接使用这里定义的方法
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class BaseService<T> : IBaseService<T> where T : BaseModel
    {
        public BaseService()
        {
            SetDAL();
        }

        protected IBaseRepository<T> dal;

        protected virtual void SetDAL()
        {
            dal = BaseRepository<T>.Current;
        }


        public T Add(T model)
        {
            return dal.Add(model);
        }
        public int Count(Expression<Func<T, bool>> predicate)
        {
            return dal.Count(predicate);
        }

        public int Update(T model)
        {
            return dal.Update(model);
        }

        //public int UpdateIgnore(Expression<Func<T, object>> propertyExpression, params T[] entities)
        //{
        //    return dal.UpdateIgnore(propertyExpression, entities);
        //}
        /// <summary>
        /// 
        /// </summary>
        /// <param name="originalEntity"></param>
        /// <param name="newEntity">只能使用匿名对象</param>
        /// <returns></returns>
        //public int Update(T originalEntity, object newEntity)
        //{
        //    return dal.Update(originalEntity, newEntity);
        //}
        public int AddOrUpdate(T model)
        {
            return dal.AddOrUpdate(model);
        }

        public void AddList(IEnumerable<T> list)
        {
            dal.InsertAll(list);
        }



        #region public
        public static BaseService<T> Current
        {
            get
            {
                var name = typeof(BaseService<T>).Name;
                var bll = CallContext.GetData(name) as BaseService<T>;
                if (bll != null)
                {
                    return bll;
                }
                bll = new BaseService<T>();
                CallContext.SetData(name, bll);
                return bll;
            }
        }
        #endregion

        #region public method        
        public void UpdateList(IEnumerable<T> list)
        {
            dal.UpdateList(list);
        }
        public int Delete(T model)
        {
            return dal.Delete(model);
        }

        public int Delete(Expression<Func<T, bool>> predicate)
        {
            return dal.Delete(predicate);
        }

        public void DeleteList(IEnumerable<T> list)
        {
            dal.DeleteList(list);
        }

        public void DeleteList(Expression<Func<T, bool>> predicate)
        {
            dal.DeleteList(predicate);
        }

        public T GetModel(Expression<Func<T, bool>> predicate = null)
        {
            return dal.GetModel(predicate);
        }
        public IEnumerable<T> GetList(Expression<Func<T, bool>> predicate = null)
        {
            return dal.GetList(predicate, false);
        }

        public List<T> GetListNoTrack(Expression<Func<T, bool>> predicate = null)
        {
            //将数据实例化
            return dal.GetList(predicate, true).ToList();
        }

        public bool Exist(Expression<Func<T, bool>> anyLambda)
        {
            return dal.Exist(anyLambda);
        }

        public T Find(Expression<Func<T, bool>> whereLambda)
        {
            return dal.Find(whereLambda);
        }

        public void InsertAll(IEnumerable<T> list)
        {
            dal.InsertAll(list);
        }

        public IQueryable<T> FindList<S>(Expression<Func<T, bool>> whereLamdba, bool isAsc, Expression<Func<T, S>> orderLamdba)
        {
            return dal.FindList(whereLamdba, isAsc, orderLamdba);
        }

        public IQueryable<T> FindPageList<S>(int pageIndex, int pageSize, out int totalRecord, Expression<Func<T, bool>> whereLamdba, bool isAsc, Expression<Func<T, S>> orderLamdba)
        {
            return dal.FindPageList(pageIndex, pageSize, out totalRecord, whereLamdba, isAsc, orderLamdba);
        }

        public IEnumerable<T> GetList(Expression<Func<T, bool>> predicate, bool v)
        {
            return dal.GetList(predicate, v);
        }
        #endregion
    }
}
